﻿using SubhadraSolutions.Sharp.Utils.Collections.ObjectModel;

namespace SubhadraSolutions.Sharp.Utils.TestApplication
{
    using System;
    using System.Collections.Generic;
    using System.Threading;
    using System.Windows.Forms;

    using SubhadraSolutions.Sharp.Utils.Collections.Concurrent;
    using SubhadraSolutions.Sharp.Utils.ComponentModel;
    using SubhadraSolutions.Sharp.Utils.Tests.DummyClasses;
    using SubhadraSolutions.Sharp.Utils.Windows;

    public static class ListNotifyCollectionChangedAdapterTestsLauncher
    {
        /// <summary>
        /// Launches this instance.
        /// </summary>
        public static void Launch()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            var form = new Form();
            form.Load += form_Load;
            Application.Run(form);
        }

        /// <summary>
        /// Handles the Load event of the form control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        static void form_Load(object sender, EventArgs e)
        {
            ThreadPool.QueueUserWorkItem(delegate { PerformDispatcherTest(); });
        }

        /// <summary>
        /// Performs the dispatcher test.
        /// </summary>
        private static void PerformDispatcherTest()
        {
            IList<Employee> list = new List<Employee>();
            list = new SafeListDecorator<Employee>(list);
            list = new DispatcherAwareBindingList<Employee>(ThreadBasedWindowsDispatcherProvider.Instance, list);

            //var list = new DispatcherAwareObservableCollection<Employee>(ThreadBasedWindowsDispatcherProvider.Instance);
            

            for (int i = 0; i < 3; i++)
            {
                ThreadPool.QueueUserWorkItem(delegate
                    {
                        var form = CreateForm(list);
                        form.ShowDialog();
                    });
            }

            Thread.Sleep(5000);
            for (int i = 0; i < 5; i++)
            {
                ThreadPool.QueueUserWorkItem(delegate(object state)
                    {
                        //while ()
                        for(int x=0;x<1000;x++)
                        {
                            var emp = Employee.BuildDummyEmployee();
                            list.Add(emp);
                            Thread.Sleep(100);
                            //list.Remove(emp);
                        }
                    });
            }
        }

        /// <summary>
        /// Creates the form.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dataSource">The data source.</param>
        /// <returns></returns>
        private static Form CreateForm<T>(IList<T> dataSource)
        {
            var form = new Form();
            var grid = new DataGridView { AllowUserToAddRows = false, AutoGenerateColumns = true, Dock = DockStyle.Fill };
            form.Controls.Add(grid);
            grid.DataSource = dataSource;
            
            return form;
        }

    }
}
